/*
 * 文件名：LocalRotaServiceImpl.java	 
 * 时     间：下午3:36:32
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.threads.UpDateCarOutInfoRunble;
import com.cloud.threads.UpDateCouponCashRunble;
import com.cloud.threads.UploadCarPayInfoRunble;
import com.cloud.threads.UploadCouponRunble;
import com.cloud.threads.UploadPayBillRunble;
import com.cz073586.tools.util.ThreadPoolUtil;
import com.local.common.PayTypeConstans;
import com.local.dao.mapers.CarLockInfoMapper;
import com.local.dao.mapers.CarParkOutMapper;
import com.local.entity.vo.ParkwellPayBillVo;
import com.local.entity.webpush.CarOutPushVo;
import com.local.manager.cfgManager.CloudConfigManager;
import com.local.manager.push.PushAdminRunble;
import com.local.manager.push.PushCarOutBillInfoRunble;
import com.local.model.AdminPayBill;
import com.local.model.CarLockInfo;
import com.local.model.CarLockInfoExample;
import com.local.model.CarParkOut;
import com.local.model.LocalCouponCash;
import com.local.model.LocalPayBill;
import com.local.service.face.CarLockInfoService;
import com.local.service.face.CrossingService;
import com.local.service.face.LogService;
import com.local.service.face.ParkwellCarOutService;
import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.Tools;
import com.qianhai.common.utils.UUIDGenerateUtil;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: LocalRotaServiceImpl
 * @author Administrator
 * @date 2015年10月3日 下午3:36:32
 */
@Service
public class CarLockInfoServiceImpl extends
		BaseServiceImpl<CarLockInfoExample, CarLockInfo, String> implements
		CarLockInfoService
{
	@Autowired
	private CarLockInfoMapper carLockInfoMapper;
	
	@Autowired
	private CarParkOutMapper carParkOutMapper;
	
	@Autowired
	private ParkwellCarOutService carOutService;
	
	@Autowired
	private CrossingService crossinService;
	
	@Autowired
	private LogService logService;
	
	@Override
	public BaseMapper<CarLockInfoExample, CarLockInfo, String> getMapper()
	{
		// TODO Auto-generated method stub
		return carLockInfoMapper;
	}
	
	/*
	 * @Override public List<LocalBlackList>
	 * queryBlackListByPage(LocalBlackListPage<LocalBlackList> page) { // TODO
	 * Auto-generated method stub Integer recordCount =
	 * localBlackListMapper.countAllByPage(page);
	 * page.setTotalRecords(recordCount); List<LocalBlackList> list =
	 * localBlackListMapper.queryByPage(page); page.setData(list); return list;
	 * }
	 */
	
	@Override
	public String saveOrUpdateCarLockInfo(CarLockInfo carLockInfo)
	{
		// TODO Auto-generated method stub
		try
		{
			if (Tools.isEmpty(carLockInfo.getUuid()))
			{
				carLockInfo.setUuid(UUIDGenerateUtil.generate());
				carLockInfoMapper.insertSelective(carLockInfo);
			} else
			{
				carLockInfoMapper.updateByPrimaryKey(carLockInfo);
			}
		} catch (Throwable e)
		{
			throw new BaseException(e.getMessage());
		}
		return carLockInfo.getUuid();
	}
	
	@Override
	public void deleteCarLockInfo(CarLockInfo carLockInfo)
	{
		// TODO Auto-generated method stub
		CarLockInfoExample condition = new CarLockInfoExample();
		condition.or().andCarNoEqualTo(carLockInfo.getCarNo());
		carLockInfoMapper.deleteByCondition(condition);
	}
	
	@Override
	public CarLockInfo queryFindByCarNo(String carNo, String parkcode)
	{
		// TODO Auto-generated method stub
		CarLockInfoExample condition = new CarLockInfoExample();
		condition.or().andCarNoEqualTo(carNo).andParkCodeEqualTo(parkcode);
		List<CarLockInfo> carLockInfoLs = carLockInfoMapper
				.selectByCondition(condition);
		return !Tools.isEmpty(carLockInfoLs) && carLockInfoLs.size() > 0 ? carLockInfoLs
				.get(0) : null;
	}
	
	/*
	 * @Override public void jdbcCall(String carNo, Integer carlock, String
	 * remark) throws Exception { // TODO Auto-generated method stub
	 * Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
	 * 
	 * String url = XltCloudConfigManager.instance().getCfgValueByKey("url");
	 * String user = XltCloudConfigManager.instance().getCfgValueByKey("user");
	 * String pwd = XltCloudConfigManager.instance().getCfgValueByKey("pwd");
	 * Connection conn = null; CallableStatement proc = null;// 执行sql存储过程的接口
	 * 
	 * // 调用存储过程SP_CarLock:向carLock表中插入数据 try { conn =
	 * DriverManager.getConnection(url, user, pwd); proc =
	 * conn.prepareCall("{call SP_CarLock(?,?,?)}"); proc.setString(1, carNo);
	 * proc.setInt(2, carlock); proc.setString(3, remark); proc.execute();
	 * System.out.println("成功调用存储过程，插入数据！"); } catch (Exception e) {
	 * e.printStackTrace(); } finally { if (proc != null) proc.close(); if (conn
	 * != null) conn.close(); } }
	 */
	
	@Override
	public CarParkOut QueryByCarNo(String carNo)
	{
		// TODO Auto-generated method stub
		try
		{
			CarParkOut cc = carParkOutMapper.QueryByCarNo(carNo);
			if (!Tools.isEmpty(cc))
			{
				return cc;
			}
		} catch (Exception e)
		{
			// TODO: handle exception
			return null;
		}
		return null;
	}
	
	@Override
	public void savePayBillLock(ParkwellPayBillVo parkwellPayBillVo)
	{
		// TODO Auto-generated method stub
		try
		{
			if (Tools.isEmpty(parkwellPayBillVo.getCarOutUuid()))
			{
				throw new BaseException("50000-00001-00039");
			}
			
			if (Tools.isEmpty(parkwellPayBillVo.getCarInUuid()))
			{
				throw new BaseException("50000-00001-00049");
			}
			
			if (Tools.isEmpty(parkwellPayBillVo.getPayType()))
			{
				parkwellPayBillVo.setPayType(PayTypeConstans.MONEY_TYPE);
			}
			
			CarParkOut carParkOut = carParkOutMapper
					.selectByPrimaryKey(parkwellPayBillVo.getCarOutUuid());
			carParkOut.setCarNo(parkwellPayBillVo.getCarNo());
			if (parkwellPayBillVo.isResult())
			{
				// 打开闸机
				if (!Tools.isEmpty(carParkOut.getEquipment()))
				{
					PushAdminRunble openRunble = new PushAdminRunble(
							new AdminPayBill(), null, carParkOut.getEquipment(),null);
					ThreadPoolUtil.instance().execute(openRunble);
				}
				
				// 出场,保证闸机开启后删除数据,推送车位数据量
				carOutService.copyToOhterTable(carParkOut.getUuid(),
						parkwellPayBillVo.getCarInUuid());
				
				// 出场
				if (CloudConfigManager.instance().isOpen())
				{
					UpDateCarOutInfoRunble upload = new UpDateCarOutInfoRunble(
							carParkOut, logService);
					ThreadPoolUtil.instance().execute(upload);
				}
				
				// 更新百事通自动扣费信息
				if (parkwellPayBillVo.isAuotPayOut()
						&& CloudConfigManager.instance().isOpen())
				{
					UploadCarPayInfoRunble upload = new UploadCarPayInfoRunble(
							parkwellPayBillVo);
					ThreadPoolUtil.instance().execute(upload);
				}
				// 上传纸质优惠券
				if (CloudConfigManager.instance().isOpen()
						&& !Tools.isEmpty(parkwellPayBillVo.getLcoupon()))
				{
					UploadCouponRunble upload = new UploadCouponRunble(
							parkwellPayBillVo.getLcoupon(), logService);
					ThreadPoolUtil.instance().execute(upload);
				}
				
				// 上传流水
				if (CloudConfigManager.instance().isOpen()
						&& !Tools.isEmpty(parkwellPayBillVo.getPayBill()))
				{
					UploadPayBillRunble upload = new UploadPayBillRunble(
							parkwellPayBillVo.getPayBill(), logService);
					ThreadPoolUtil.instance().execute(upload);
				}
				
				// 生成的流水，同步到云平台
				if (CloudConfigManager.instance().isOpen())
				{
					for (LocalPayBill cnpBill : parkwellPayBillVo
							.getCouponToPayBillsList())
					{
						UploadPayBillRunble upload = new UploadPayBillRunble(
								cnpBill, logService);
						ThreadPoolUtil.instance().execute(upload);
					}
					
					// 使用的商家电子优惠劵记录，上传到云系统，并修改使用状态
					for (LocalCouponCash lc : parkwellPayBillVo.getLscList())
					{
						UpDateCouponCashRunble upload = new UpDateCouponCashRunble(
								lc, logService);
						ThreadPoolUtil.instance().execute(upload);
					}
				}
			}
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
		}
	}
	
	@Override
	public ParkwellPayBillVo addCarParkOutBillLock(CarParkOut carParkOut)
	{
		// TODO Auto-generated method stub
		ParkwellPayBillVo parkwellPayBillVo = new ParkwellPayBillVo();
		int price = 0;
		try
		{
			carOutService.carOut(carParkOut, parkwellPayBillVo);
		} catch (Throwable e)
		{
		} finally
		{
			
			// 返回相机信息
			price = !Tools.isEmpty(parkwellPayBillVo)
					&& !Tools.isEmpty(parkwellPayBillVo.getPriceDifference()) ? parkwellPayBillVo
					.getPriceDifference().intValue() : 0;
			if (!Tools.isEmpty(parkwellPayBillVo))
			{
				// 推送到前端页面
				CarOutPushVo carOutPushVo = new CarOutPushVo();
				carOutPushVo.setCarNo(carParkOut.getCarNo());
				carOutPushVo.setCarNoTye(parkwellPayBillVo.getCarNoTye());
				carOutPushVo.setCarOutUuid(carParkOut.getUuid());
				carOutPushVo.setEuqmentNo(carParkOut.getEquipment());
				if (parkwellPayBillVo.isResult())
				{
					carOutPushVo.setIsOpenGate("1");
				} else
				{
					carOutPushVo.setIsOpenGate("2");
				}
				
				PushCarOutBillInfoRunble pushToWeb = new PushCarOutBillInfoRunble(
						crossinService, carOutPushVo);
				ThreadPoolUtil.instance().execute(pushToWeb);
			}
		}
		return parkwellPayBillVo;
	}
	
}
